package com.zsinfo.guoss.supplier.fragment;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.example.netcenter.Event.Response;
import com.example.netcenter.NetCenter;
import com.example.netcenter.RetrofitBuilder;
import com.example.netcenter.Url.ConstantsCode;
import com.example.netcenter.bean.BillingBean;
import com.example.netcenter.bean.ResultsList;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;
import com.zsinfo.guoss.supplier.R;
import com.zsinfo.guoss.supplier.activity.BillingDetailsActivity;
import com.zsinfo.guoss.supplier.base.BaseFragment;
import com.zsinfo.guoss.supplier.utils.SharedPreferencesUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lixl on 2018/12/5.
 * <p>
 * desc:国产类 账单明细列表
 */
public class ChainBillingFragment extends BaseFragment {
    private LinearLayout ll_empty;
    private XRecyclerView xRecyclerView;
    private CommonAdapter billingAdapter;
    private ResultsList<BillingBean> resultsLists;  //服务器返回的数据
    private List<BillingBean> list = new ArrayList<>();  //列表数据
    private int pageNo = 1;
    private int pageSize = 10;
    private boolean isLast = false; //是否是最后一条数据
    private boolean isRefresh = false, isLoadMore = false; //默认不刷新，不加载

    @Override
    protected int getContentLayoutRes() {
        return R.layout.fragment_billing_list;
    }

    @Override
    protected void initView(View view) {

        ll_empty = view.findViewById(R.id.ll_empty);
        xRecyclerView = view.findViewById(R.id.recyclerview);

        setAdapter();

        initNetData();
    }

    protected void initNetData() {
        new NetCenter().GetNetCenter(RetrofitBuilder.CreateApi().SUPPLIER_BILL_LIST(
                        ConstantsCode.supplier_bill_list,
                        SharedPreferencesUtil.getID(),
                        "1", pageNo + "", pageSize + ""), new Response() {
                    @Override
                    public void OK(Object data) {
                        Log.e("lixl", data.toString());
                        setDataView(data);
                    }

                    @Override
                    public void Fail(Throwable e) {
                        Log.e("lixl", e.getMessage());
                    }

                    @Override
                    public void complete() {
                        if (isRefresh) {
                            xRecyclerView.refreshComplete();
                        }
                        if (isLoadMore) {
                            xRecyclerView.loadMoreComplete();
                        }
                    }
                });
    }

    private void setAdapter() {
        xRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        xRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        xRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        xRecyclerView.setArrowImageView(R.mipmap.iconfont_downgrey);
        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                //清除数据
                list.clear();
                //更新适配器
                billingAdapter.notifyDataSetChanged();
                //刷新操作
                pageNo = 1;
                //重新请求接口
                initNetData();
                //正在执行刷新
                isRefresh = true;
            }

            @Override
            public void onLoadMore() {
                //可以判断 isLast=true，表示已经没有更多数据了
                if (isLast) {
                    showToast("当前已经没有更多数据了");
                    xRecyclerView.loadMoreComplete(); //停止加载的动画效果
                    return;
                }
                //加载更多
                pageNo++;
                //请求加载接口
                initNetData();
                //正在执行加载
                isLoadMore = true;
            }
        });
        billingAdapter = new CommonAdapter<BillingBean>(getActivity(), R.layout.item_billing, list) {
            @Override
            protected void convert(ViewHolder holder, final BillingBean billingBean, int position) {
                int type = billingBean.getType();
                int money = billingBean.getMoney();
                if (type == 1){
                    holder.setText(R.id.tv_type, "缴纳");
                    holder.setText(R.id.tv_money, "+" + money + "元");
                    holder.setText(R.id.tv_pay_method_title, "付款方式：");
                    holder.setText(R.id.tv_pay_method, billingBean.getPayMethod());
                    holder.setText(R.id.tv_end_time_title, "到账时间：");
                    holder.setText(R.id.tv_end_time, billingBean.getPayTime());

                } else if (type == 2){
                    holder.setText(R.id.tv_type, "退款");
                    holder.setText(R.id.tv_money, "-" + money + "元");
                    holder.setText(R.id.tv_pay_method_title, "退款进度：");
                    int status = billingBean.getStatus();
                    if (status == 1){
                        holder.setText(R.id.tv_pay_method, "处理中");
                        holder.setText(R.id.tv_end_time_title, "到账时间：");
                        holder.setText(R.id.tv_end_time, "--");
                    } else if (status == 2){
                        holder.setText(R.id.tv_pay_method, "到账成功");
                        holder.setText(R.id.tv_end_time_title, "到账时间：");
                        holder.setText(R.id.tv_end_time, billingBean.getPayTime());
                    } else {  // -1
                        holder.setText(R.id.tv_pay_method, "退款失败");
                        holder.setText(R.id.tv_end_time_title, "失败原因：");
                        holder.setText(R.id.tv_end_time, billingBean.getReason());
                    }

                } else if (type == 3){
                    holder.setText(R.id.tv_type, "扣款");
                    holder.setText(R.id.tv_money, "-" + money + "元");
                    holder.setText(R.id.tv_pay_method_title, "扣款原因：");
                    holder.setText(R.id.tv_pay_method, billingBean.getReason());
                    holder.setText(R.id.tv_end_time_title, "扣款时间：");
                    holder.setText(R.id.tv_end_time, billingBean.getPayTime());
                }
            }
        };
        billingAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                position--; //防止数组下标越界
                Intent i = new Intent(getActivity(), BillingDetailsActivity.class);
                i.putExtra("id", list.get(position).getId());
                startActivity(i);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        xRecyclerView.setAdapter(billingAdapter);
    }

    /**
     * 设置数据到view
     *
     * @param data
     */
    private void setDataView(Object data) {
        resultsLists = (ResultsList<BillingBean>) data;
        //赋值
        isLast = resultsLists.getData().isIsLast();
        //判断一下，currentPageNo==1是表示第一次加载数据，如果第一次加载数据为空，就表示没有数据
        List<BillingBean> datas = resultsLists.getData().getObjects();
        if (pageNo == 1) {
            if (datas == null || datas.size() == 0) {
                xRecyclerView.setVisibility(View.GONE);
                ll_empty.setVisibility(View.VISIBLE);
                return;
            } else {
                xRecyclerView.setVisibility(View.VISIBLE);
                ll_empty.setVisibility(View.GONE);
            }
        }
        //更新适配器
        list.addAll(datas);
        billingAdapter.notifyDataSetChanged();
    }
}
